home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / peak / util / plugins.pyo (.txt) < prev   
Python Compiled Bytecode  |  2008-10-13  |  3KB  |  104 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import pkg_resources
  5. from peak.util.decorators import struct
  6. from peak import context
  7. __all__ = [
  8.     'Hook',
  9.     'Extensible',
  10.     'PluginManager']
  11.  
  12. def additional_tests():
  13.     import doctest as doctest
  14.     return doctest.DocFileSuite('README.txt', package = '__main__', optionflags = doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE)
  15.  
  16.  
  17. class Extensible(object):
  18.     __slots__ = ()
  19.     extend_with = ()
  20.     
  21.     def load_extensions(self):
  22.         for ext in _flatten_callables(self.extend_with):
  23.             ext(self)
  24.         
  25.  
  26.  
  27.  
  28. def _flatten_callables(ob):
  29.     if callable(ob):
  30.         yield ob
  31.     else:
  32.         for sub_ob in ob:
  33.             for ob in _flatten_callables(sub_ob):
  34.                 yield ob
  35.             
  36.         
  37.  
  38.  
  39. class Hook(object):
  40.     __slots__ = ()
  41.     
  42.     def __iter__(self):
  43.         return PluginManager.iterHooks(self.group, self.impl)
  44.  
  45.     
  46.     def register(self, ob, impl = None):
  47.         if impl and self.impl and impl != self.impl:
  48.             raise ValueError('Can only register ' + self.impl + ' implementations')
  49.         
  50.         if not impl:
  51.             pass
  52.         return PluginManager.addEntryPoint(self.group, self.impl, ob)
  53.  
  54.     
  55.     def notify(self, *args, **kw):
  56.         for hook in self.query(*args, **kw):
  57.             pass
  58.         
  59.  
  60.     
  61.     def query(self, *args, **kw):
  62.         if kw:
  63.             for hook in self:
  64.                 yield hook(*args, **kw)
  65.             
  66.         elif args:
  67.             for hook in self:
  68.                 yield hook(*args)
  69.             
  70.         else:
  71.             for hook in self:
  72.                 yield hook()
  73.             
  74.  
  75.  
  76. struct(Hook)
  77.  
  78. def Hook(group, impl = None):
  79.     return (group, impl)
  80.  
  81. _implementations = { }
  82.  
  83. class PluginManager(context.Service):
  84.     
  85.     def addEntryPoint(self, group, impl, ob):
  86.         _implementations.setdefault(group, []).append((impl, ob))
  87.  
  88.     
  89.     def iterHooks(self, group, impl = None, project = None):
  90.         if project:
  91.             project = project.lower()
  92.         
  93.         for name, ob in _implementations.get(group, ()):
  94.             if impl and name != impl:
  95.                 continue
  96.             
  97.             yield ob
  98.         
  99.         for ep in pkg_resources.iter_entry_points(group, impl):
  100.             yield ep.load()
  101.         
  102.  
  103.  
  104.